
let obj = {
    name: 'ljp',
    add(a, b) {
        console.log(this.name, a, b)
    }
}

const obj1 = {
    name: 'xxx'
}

obj.add(1, 2)

obj.add.call(obj1, 1, 2)

Function.prototype.myCall = function (ctx, ...args) {
    ctx = ctx || window
    let result
    if (typeof this === 'function') {
        ctx.fn = this
        result = ctx.fn(...args)
    }
    delete ctx.fn
    return result
}


obj.add.myCall(obj1, 1, 2)


/***
 *  将call传入的ctx作为原型 
 *  将实例（this）挂在ctx上
 *  执行ctx.fn  将ctx.fn执行结果返回  
 *  并还原ctx 即 删除ctx.fn
 * 
 */